package de.klingbeil.hhc.client.widget;

import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.Viewport;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.google.gwt.user.client.ui.RootLayoutPanel;
import com.google.gwt.user.client.ui.Widget;

public class RootContainer {

	private Viewport viewport;
	private LayoutContainer centerContainer;

	public RootContainer() {
		viewport = new Viewport();
		viewport.setLayout(new BorderLayout());
		RootLayoutPanel.get().add(viewport);
		addCenterContainer();
	}

	private void addCenterContainer() {
		centerContainer = new LayoutContainer();
		centerContainer.setLayout(new BorderLayout());
		BorderLayoutData layoutData = new BorderLayoutData(LayoutRegion.CENTER);
		layoutData.setMargins(new Margins(-32, 6, 6, 6));
		viewport.add(centerContainer, layoutData);
	}

	private static BorderLayoutData createHeaderLayoutData(int headerHeight) {
		return new BorderLayoutData(LayoutRegion.NORTH, headerHeight);
	}

	private static BorderLayoutData createActionBarLayoutData() {
		return new BorderLayoutData(LayoutRegion.NORTH);
	}

	private static BorderLayoutData createEditorLayoutData() {
		return new BorderLayoutData(LayoutRegion.CENTER);
	}

	private static BorderLayoutData createListLayoutData() {
		return new BorderLayoutData(LayoutRegion.SOUTH);
	}

	private static BorderLayoutData createInfoLayoutData() {
		BorderLayoutData layoutData = new BorderLayoutData(LayoutRegion.EAST,
				230);
		layoutData.setMargins(new Margins(-32, 6, 6, 6));
		return layoutData;
	}

	public void setHeaderView(Widget widget, int headerHeight) {
		viewport.add(widget, createHeaderLayoutData(headerHeight));
		viewport.layout();
	}

	public void setInfoView(Widget widget) {
		viewport.add(widget, createInfoLayoutData());
		viewport.layout();
	}

	public void setEditorView(Widget widget) {
		centerContainer.add(widget, createEditorLayoutData());
		centerContainer.layout();
	}

	public void setListView(Widget widget) {
		centerContainer.add(widget, createListLayoutData());
		centerContainer.layout();
	}

	public void setActionBarView(Widget widget) {
		centerContainer.add(widget, createActionBarLayoutData());
		centerContainer.layout();
	}

}
